package com.dxl.system.controller;

import com.dxl.base.Result;
import com.dxl.system.model.Note;
import com.dxl.system.model.Series;
import com.dxl.system.service.NoteService;
import com.dxl.system.service.SeriesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <b> 系列控制器
 * </b><br><br><i>Description</i> :
 * <br>Date: 2019-09-25    <br>Author : dxl
 */
@Api(value = "系列接口",tags = {"系列--接口"})
@Controller
@RequestMapping("/series")
public class SeriesController {

    @Autowired(required = false)
    private SeriesService seriesService;
    @Autowired(required = false)
    private NoteService noteService;


    /**
     * <b> 插入系列
     * </b><br><br><i>Description</i> : 不能为空：系列名称-series_name ；不能重复：系列名称-series_name  
     * @param series Series
     * @return com.dxl.base.Result Series
     * <br><br>Date: 2019-09-25    <br>Author : dxl
     */
    @ApiOperation(value = "插入系列",notes = "不能为空：系列名称-series_name ；不能重复：系列名称-series_name  ")
    @RequestMapping(value = "/addOne",method = {RequestMethod.POST })
    @ResponseBody
    public Result addOne(@Valid @RequestBody Series series) {
        Result result = seriesService.addOne(series);
        return result;
    }

    /**
     * <b> 插入系列Html
     * </b><br><br><i>Description</i> : 不能为空：系列名称-series_name ；不能重复：系列名称-series_name
     * @return String
     * <br><br>Date: 2019-09-25    <br>Author : dxl
     */
    @ApiOperation(value = "插入系列Html",notes = "不能为空：系列名称-series_name ；不能重复：系列名称-series_name  ")
    @RequestMapping(value = "/addOneHtml",method = {RequestMethod.GET })
    public String addOneHtml() {
        return "series/series_add";
    }

    /**
     * <b> 删除系列
     * </b><br><br><i>Description</i> : 
     * @param seriesIds List: Integer
     * @return com.dxl.base.Result int
     * <br><br>Date: 2019-09-25    <br>Author : dxl
     */
    @ApiOperation(value = "删除系列",notes = "无注意点！")
    @RequestMapping(value = "/delByIds",method = {RequestMethod.POST })
    @ResponseBody
    public Result delByIds(@RequestParam(value="seriesIds", required = true) List<Integer> seriesIds){
        Result result = seriesService.delByIds(seriesIds);
        return result;
    }

    /**
     * <b> 删除系列及文件
     * </b><br><br><i>Description</i> :
     * @param series
     * @return com.dxl.base.Result int
     * <br><br>Date: 2019-09-25    <br>Author : dxl
     */
    @ApiOperation(value = "删除系列",notes = "无注意点！")
    @RequestMapping(value = "/delJoinByIds",method = {RequestMethod.POST })
    @ResponseBody
    public Result delJoinByIds(@Valid @RequestBody Series series){
        Series series1 = new Series();
        series1.setSeriesName(series.getSeriesName());
        List<Series> seriesList = (List<Series>) seriesService.getList(series1).getData();
        List<Integer> seriesIds = new ArrayList<>();
        seriesIds.add(seriesList.get(0).getId());
        Result result = seriesService.delByIds(seriesIds);
        Note note = new Note();
        note.setSeriesId(seriesList.get(0).getId());
        List<Note> noteList = (List<Note>) noteService.getList(note).getData();
        List<Integer> noteIds = noteList.stream().map(Note::getId).collect(Collectors.toList());
        if(noteIds.size() > 0){
            noteService.delAndFile(noteIds);
        }
        return result;
    }

    /**
     * <b> 编辑系列Html
     * </b><br><br><i>Description</i> : 不能重复：系列名称-series_name 
     * @param seriesName String
     * @return com.dxl.base.Result Series
     * <br><br>Date: 2019-09-25    <br>Author : dxl
     */
    @ApiOperation(value = "编辑系列Html",notes = "不能重复：系列名称-series_name ")
    @RequestMapping(value = "/editOneHtml",method = {RequestMethod.GET })
    public String editOneHtml(@RequestParam(value="seriesName", required = true) String seriesName,Model model) {
        Series series2 = new Series();
        series2.setSeriesName(seriesName);
        List<Series> seriesList = (List<Series>) seriesService.getList(series2).getData();
        Series series = seriesList.get(0);
        model.addAttribute(series);
        return "series/series_edit";
    }

    /**
     * <b> 编辑系列
     * </b><br><br><i>Description</i> : 不能重复：系列名称-series_name
     * @param series Series
     * @return com.dxl.base.Result Series
     * <br><br>Date: 2019-09-25    <br>Author : dxl
     */
    @ApiOperation(value = "编辑系列",notes = "不能重复：系列名称-series_name ")
    @RequestMapping(value = "/editOne",method = {RequestMethod.POST })
    @ResponseBody
    public Result editOne(@Valid @RequestBody Series series) {
        Result result = seriesService.editOne(series);
        return result;
    }

    /**
     * <b> ID查询系列
     * </b><br><br><i>Description</i> : 
     * @param seriesId Integer
     * @return com.dxl.base.Result: Series
     * <br><br>Date: 2019-09-25    <br>Author : dxl
     */
    @ApiOperation(value = "ID查询系列",notes = "无注意点！")
    @RequestMapping(value = "/getById",method = {RequestMethod.POST })
    @ResponseBody
    public Result getById(@RequestParam(value="seriesId", required = true) Integer seriesId) {
        Result result = seriesService.getById(seriesId);
        return result;
    }

    /**
     * <b> 条件查询系列
     * </b><br><br><i>Description</i> : 
     * @param series Series
     * @return com.dxl.base.Result List: Series
     * <br><br>Date: 2019-09-25    <br>Author : dxl
     */
    @ApiOperation(value = "条件查询系列",notes = "无注意点！")
    @RequestMapping(value = "/getList",method = {RequestMethod.POST })
    @ResponseBody
    public Result getList(@Valid @RequestBody Series series) {
        Result result = seriesService.getList(series);
        return result;
    }

    /**
     * <b> 分页条件查询系列
     * </b><br><br><i>Description</i> : 
     * @param series Series, currentPage -Integer
     * @return com.dxl.base.Result List: Series
     * <br><br>Date: 2019-09-25     <br>Author : dxl
     */
    @ApiOperation(value = "分页条件查询系列",notes = "无注意点！")
    @RequestMapping(value = "/getListByPage",method = {RequestMethod.POST })
    @ResponseBody
    public Result getListByPage(@Valid @RequestBody Series series, @RequestParam(value="currentPage", required = false, defaultValue = "1") Integer currentPage, @RequestParam(value="pageSize", required = false, defaultValue = "20") Integer pageSize) {
        Result result = seriesService.getListByPage(series, currentPage, pageSize);
        return result;
    }

    /**
     * <b>  模糊查询系列
     * </b><br><br><i>Description</i> : 模糊查询；模糊字段(模式：单字段)：全匹配：series_name-系列名称 
     * @param series Series
     * @return com.dxl.base.Result List: Series
     * <br><br>Date: 2019-09-25    <br>Author : dxl
     */
    @ApiOperation(value = "模糊查询系列",notes = "模糊查询；模糊字段(模式：单字段)：全匹配：series_name-系列名称 ")
    @RequestMapping(value = "/getLikeSeries",method = {RequestMethod.POST })
    @ResponseBody
    public Result getLikeSeries(@Valid @RequestBody Series series){
        Result result = seriesService.getLikeSeries(series);
        return result;
    }

    /**
     * <b> 分页模糊查询系列
     * </b><br><br><i>Description</i> : 模糊查询；模糊字段(模式：单字段)：全匹配：series_name-系列名称 
     * @param series Series, currentPage -Integer, pageSize -Integer
     * @return com.dxl.base.Result List: Series
     * <br><br>Date: 2019-09-25     <br>Author : dxl
     */
    @ApiOperation(value = "分页模糊查询系列",notes = "模糊查询；模糊字段(模式：单字段)：全匹配：series_name-系列名称 ")
    @RequestMapping(value = "/getLikeSeriesByPage",method = {RequestMethod.POST })
    @ResponseBody
    public Result getLikeSeriesByPage(@Valid @RequestBody Series series, @RequestParam(value="currentPage", required = false, defaultValue = "1") Integer currentPage, @RequestParam(value="pageSize", required = false, defaultValue = "20") Integer pageSize){
        Result result = seriesService.getLikeSeriesByPage(series, currentPage, pageSize);
        return result;
    }

    /**
     * <b> 系列能否新增
     * </b><br><br><i>Description</i> : 无权限限制；不能为空：系列名称-series_name ；不能重复：系列名称-series_name  
     * @param series Series
     * @return com.dxl.base.Result boolean
     * <br><br>Date: 2019-09-25     <br>Author : dxl
     */
    @ApiOperation(value = "系列能否新增",notes = "无权限限制；不能为空：系列名称-series_name ；不能重复：系列名称-series_name  ")
    @RequestMapping(value = "/isAddable",method = {RequestMethod.POST })
    @ResponseBody
    public Result isAddable(@Valid @RequestBody Series series) {
        Result result = seriesService.isSeriesAddable(series);
        return result;
    }

    /**
     * <b> 系列能否编辑
     * </b><br><br><i>Description</i> : 无权限限制；不能重复：系列名称-series_name 
     * @param series Series
     * @return com.dxl.base.Result boolean
     * <br><br>Date: 2019-09-25     <br>Author : dxl
     */
    @ApiOperation(value = "系列能否编辑",notes = "无权限限制；不能重复：系列名称-series_name ")
    @RequestMapping(value = "/isEditable",method = {RequestMethod.POST })
    @ResponseBody
    public Result isEditable(@Valid @RequestBody Series series) {
        Result result = seriesService.isSeriesEditable(series);
        return result;
    }

    /**
     * <b> 分页条件查询系列中文件
     * </b><br><br><i>Description</i> :
     * @param series Series, currentPage -Integer
     * @return com.dxl.base.Result List: Series
     * <br><br>Date: 2019-09-25     <br>Author : dxl
     */
    @ApiOperation(value = "分页条件查询系列中文件",notes = "无注意点！")
    @RequestMapping(value = "/getAllNoteInSeriesByNameByPage",method = {RequestMethod.POST })
    @ResponseBody
    public Result getAllNoteInSeriesByNameByPage(@Valid @RequestBody Series series, @RequestParam(value="currentPage", required = false, defaultValue = "1") Integer currentPage, @RequestParam(value="pageSize", required = false, defaultValue = "20") Integer pageSize) {

        Result result = seriesService.getAllNoteInSeriesByNameByPage(series, currentPage, pageSize);
        return result;
    }
}
